package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.util.BeanMapper;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 主要海洋产业生产率上升率评估指标计算类.
 */
public class MajorMarineIndustryProductivityRiseRateLastCalculateService extends IndicatorEvaluationCalculateService {

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = Double.valueOf(0);
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			result = calculateResult(indicator, indicatorAccountVO);
		}
		return result;

	}

	private Double calculateResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {

		Double addValueOfIndustry = getAddValueOfIndustry(indicatorAccountVO, indicator);
		Double empOfIndustry = getEmpOfIndustry(indicatorAccountVO, indicator);

		IndicatorAccountVO lastVO = BeanMapper.map(indicatorAccountVO, IndicatorAccountVO.class);
		lastVO.setYear(indicatorAccountVO.getYear() - 1);
		Double lastAddValueOfIndustry = getAddValueOfIndustry(lastVO, indicator);
		Double lastEmpOfIndustry = getEmpOfIndustry(lastVO, indicator);

		Validate.isTrue(!(Double.valueOf(0).equals(empOfIndustry)));
		Validate.isTrue(!(Double.valueOf(0).equals(lastEmpOfIndustry)));

		Double firstVt = empOfIndustry == 0 ? 0 : addValueOfIndustry / empOfIndustry;
		Double firstV0 = lastEmpOfIndustry == 0 ? 0 : lastAddValueOfIndustry / lastEmpOfIndustry;

		Validate.isTrue(!(Double.valueOf(0).equals(firstV0)));
		return firstV0 == 0 ? 0 : firstVt / firstV0;
	}

	/**
	 * 获得省级十个主要海洋产业的增加值
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getAddValueOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_ADD_VALUE_MAP.get(indicator
				.getIndustryCode());
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), industryName);
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

	/**
	 * 获得省级十个主要海洋产业的劳动力人数
	 * 
	 * @param indicatorAccountVO
	 * @param indicator
	 * @return
	 */
	private Double getEmpOfIndustry(IndicatorAccountVO indicatorAccountVO, IndicatorEvaluation indicator) {
		String industryName = ProvinceIndicatorConstant.MAJOR_MARINE_INDUSTRY_MAP.get(indicator
				.getIndustryCode());
		Double inputOrAccountingIndicatorValue = getInputOrAccountingIndicatorValue(indicatorAccountVO,
				indicatorAccountVO.getYear(), industryName
						+ "劳动力人数");
		Validate.isTrue(inputOrAccountingIndicatorValue != null);
		return inputOrAccountingIndicatorValue;
	}

}
